package com.sw.test01.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sw.test01.R;
import com.sw.test01.domain.MyDataBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class JsonActivity extends AppCompatActivity {
    private Toolbar tbToolbar;
    private Button btnJson;
    private Button btnGson;
    private TextView tvResult;

    private static final String TAG = "JsonActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);
        tbToolbar = (Toolbar) findViewById(R.id.tb_toolbar);
        btnJson = (Button) findViewById(R.id.btn_jsonObject);
        btnGson = (Button) findViewById(R.id.btn_gson);
        tvResult = (TextView) findViewById(R.id.tv_result);

        tbToolbar.setTitle("json解析");

        btnJson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            OkHttpClient client = new OkHttpClient();
                            Request request = new Request.Builder()
                                    .url("http://192.168.0.168:8080/parseJson/get_data.json")
                                    .build();
                            Response response = client.newCall(request).execute();
                            String responseData = response.body().string();
                            Log.d(TAG, "json:" + responseData);
                            parseJsonWithJSONObject(responseData);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

        btnGson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            OkHttpClient client = new OkHttpClient();
                            Request request = new Request.Builder()
                                    .url("http://192.168.0.168:8080/parseJson/get_data.json")
                                    .build();
                            Response response = client.newCall(request).execute();
                            String responseData = response.body().string();
                            parseJsonWithGson(responseData);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }

    /**
     * gson解析
     */
    private void parseJsonWithGson(String jsonData) {
        final StringBuilder sb = new StringBuilder();
        Gson gson = new Gson();
        List<MyDataBean> data = gson.fromJson(jsonData, new TypeToken<List<MyDataBean>>() {
        }.getType());
        for (MyDataBean dataBean : data){
            Log.d(TAG, "id is = " + dataBean.getId());
            Log.d(TAG, "name is = " + dataBean.getName());
            Log.d(TAG, "version is = " + dataBean.getVersion());
            sb.append(dataBean.getId() + ":" + dataBean.getName() + ":" + dataBean.getVersion() + "\n");
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String str = sb.toString();
                tvResult.setText(str);
            }
        });
    }

    /**
     * 原生解析
     *
     * @param jsonData 数据
     */
    private void parseJsonWithJSONObject(String jsonData) {
        final StringBuilder sb = new StringBuilder();
        try {
            JSONArray jsonArray = new JSONArray(jsonData);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id = jsonObject.getString("id");
                String name = jsonObject.getString("name");
                String version = jsonObject.getString("version");
                Log.d(TAG, "id is = " + id);
                Log.d(TAG, "name is = " + name);
                Log.d(TAG, "version is = " + version);
                sb.append(id + ":" + name + ":" + version + "\n");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String str = sb.toString();
                    tvResult.setText(str);
                }
            });
        }
    }
}
